package com.yiqi.news.mvp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.support.v7.widget.DefaultItemAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.OnClick

import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.jess.arms.widget.DividerItemDecoration
import com.novel.cn.ext.dp2px
import com.yiqi.huize.loadImage
import com.yiqi.huize.mvp.dialog.OpenPageDialog

import com.yiqi.news.di.component.DaggerExchangeComponent
import com.yiqi.news.di.module.ExchangeModule
import com.yiqi.news.mvp.contract.ExchangeContract
import com.yiqi.news.mvp.presenter.ExchangePresenter

import com.yiqi.news.R
import com.yiqi.news.app.widget.qmui.QMUITipDialog
import com.yiqi.news.entity.BankCardVO
import com.yiqi.news.entity.Comment
import com.yiqi.news.mvp.ui.adapter.LabelAdapter
import kotlinx.android.synthetic.main.activity_exchange.*
import kotlinx.android.synthetic.main.dialog_bottom_sheet_label.view.*
import kotlinx.android.synthetic.main.include_title.*
import kotlinx.android.synthetic.main.include_title.view.*
import kotlinx.android.synthetic.main.item_bankcard_footer.view.*

class ExchangeActivity : BaseActivity<ExchangePresenter>(), ExchangeContract.View, View.OnClickListener, OpenPageDialog.OnClickListener {


    private val mGoldNow: Int by lazy {
        return@lazy intent.getIntExtra("gold_now", 0)
    }

    override fun showDeleteBankSuccess() {
        mBatchAdapter.remove(mSelectPosition)
    }

    override fun showWithdrawSuccess() {
        mQMUIInfoDialog.show()
    }

    private val mQMUIInfoDialog: QMUITipDialog by lazy {
        return@lazy QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("提现申请成功")
                .create()
    }


    private lateinit var mBankData: BankCardVO

    private var mSelectPosition: Int = -1

    override fun onOpen() {
        mBatchAdapter.getItem(mSelectPosition)?.id?.let { mPresenter?.deleteBank(it) }
    }

    override fun showMyBankcardList(cardList: List<BankCardVO>) {
        mBatchAdapter.setNewData(cardList)
        mBottomSheetDialog.show()
    }

    override fun showMyBankcardError(cardList: String) {
        showMessage(cardList)
    }

    private val mOpenPageDialog by lazy {
        return@lazy OpenPageDialog(this)
    }

    private lateinit var mView: View

    private val mBottomSheetDialog: BottomSheetDialog by lazy {
        return@lazy BottomSheetDialog(this)
    }

    private val mBatchAdapter: LabelAdapter by lazy {
        return@lazy LabelAdapter()
    }


    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerExchangeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .exchangeModule(ExchangeModule(this))
                .build()
                .inject(this)

        val num = 3

    }


    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_exchange //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    override fun initData(savedInstanceState: Bundle?) {
        toolbar.toolbar_title.text = "提现兑换"
        toolbar.toolbar_menu.text = "规则说明"

        toolbar.toolbar_menu.setOnClickListener { ArmsUtils.startActivity(RulesActivity::class.java) }
        initBottomSheetDialog()

        tv_ex_now.text = "当前金币数$mGoldNow"
    }

    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {
        finish()
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(R.id.toolbar).init()
    }


    @OnClick(R.id.toolbar_back, R.id.btn_withdraw_bankcard, R.id.btn_withdraw_exchange)
    override fun onClick(view: View) {
        when (view.id) {
            R.id.btn_withdraw_bankcard -> {
                mPresenter?.doGetBanklist()
            }

            R.id.btn_withdraw_exchange -> mPresenter?.addCashDrawsRecord(mBankData.id)
        }
    }

    override fun initEvent() {
        mOpenPageDialog.setDesc("是否删除")
        mOpenPageDialog.setOnClickListener(this)
    }


    private fun initBottomSheetDialog() {
        mView = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_sheet_label, null, false)
        mView.recycler_view.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(dp2px(4)))
        mBatchAdapter.bindToRecyclerView(mView.recycler_view)
        mBatchAdapter.addFooterView(getHeaderView())
        mBottomSheetDialog.setContentView(mView)
        (mView.parent as View).setBackgroundColor(resources.getColor(android.R.color.transparent))
        mView.tv_dialog_close.setOnClickListener { mBottomSheetDialog.dismiss() }
        mBatchAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.btnDelete -> {
                    mSelectPosition = position
                    mOpenPageDialog.show()
                }
                R.id.btn_select_bank -> {
                    mBankData = mBatchAdapter.getItem(position)!!
                    iv_exchange_logo.loadImage(mBankData.accBankImg)
                    tv_exchange_name.text = mBankData.accBankName
                    btn_withdraw_exchange.isEnabled = true
                    mBottomSheetDialog.dismiss()
                }
            }

        }
    }


    private fun getHeaderView(): View {
        var view = layoutInflater.inflate(R.layout.item_bankcard_footer, mView.recycler_view.parent as ViewGroup, false)
        view.iv_bank_add.setOnClickListener {
            ArmsUtils.startActivity(BankcardActivity::class.java)
            mBottomSheetDialog.dismiss()
        }

        return view
    }
}
